package net.esj.basic.validate.defaults;

import java.util.Locale;

import javax.servlet.http.HttpSession;
import javax.validation.MessageInterpolator;

import net.esj.basic.core.view.filter.I18NFilter;

import org.apache.struts2.ServletActionContext;

public class EntityMessageInterpolator implements MessageInterpolator {

	private MessageInterpolator messageInterpolator;
	
	protected EntityMessageInterpolator(MessageInterpolator messageInterpolator) {
		super();
		this.messageInterpolator = messageInterpolator;
	}

	@Override
	public String interpolate(String arg0, Context arg1) {
		HttpSession session = ServletActionContext.getRequest().getSession();
		if(session!=null){
			Locale locale = (Locale) session.getAttribute(I18NFilter.STRUTS_I18N_KEY);
			if(locale!=null){
				return messageInterpolator.interpolate(arg0, arg1,locale);
			}
		}
		return messageInterpolator.interpolate(arg0, arg1);
	}

	@Override
	public String interpolate(String arg0, Context arg1, Locale arg2) {
		return messageInterpolator.interpolate(arg0, arg1, arg2);
	}

}
